<?php

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/4/20 0020
 * Time: 下午 2:41
 */
class Category_model extends Base_Model
{

    public function __construct()
    {
        $this->load->database();
    }


    /**
     * 获取活动
     */
    public function all_categories($data = array())
    {
        if(!empty($data['$offset']) && !empty($data['limit']))
        {
            $this->db->limit($data['limit'],$data['$offset']);
        }

        if(!empty($data['parentId']))
        {

            $this->db->where('parentId', $data['parentId']);
        }

        if(!empty($data['select']))
        {
            $this->db->select("{$data['select']}");
        }

        if(!empty($data['type']))
        {

            $this->db->where('type', $data['type']);
        }

        $query = $this->db->get('tiyu_category')->result_array();
        if(!empty($data['select']))
        {
            return $query;
        }


        $ret = array();
        $names = array();
        foreach ($query as $rs)
        {
            $id   = $rs['id'];
            $type = $rs['type'];
            $pid  = $rs['parentId'];

            if ($type == 'MAINCAT')
            {
                $ret[$id] = array(
                    'info' => $rs,
                    'child' => array(),
                );

                $names[$id] = $rs['name'];
            }
            else
            {
                $ret[$pid]['child'][] = $rs;
            }
        }

        if (!empty($data['return_structure']) && $data['return_structure'])
            return $ret;

        $result = array();
        foreach ($ret as $pid => $pinfo)
        {
            foreach ($pinfo['child'] as $child)
            {
                $child['type'] = '子分类';
                $child['parentId'] = isset($names[$child['parentId']])?$names[$child['parentId']]:'';

                $result[] = $child;
            }
        }

        return $result;
    }


    public function  getType($data=array())
    {

    }

}